package com.zkk.web;

import com.zkk.pojo.User;
import com.zkk.service.UserService;
import com.zkk.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import static java.lang.Integer.parseInt;

@WebServlet("/findAll")
public class findAll extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
       // HttpSession session = req.getSession();//获取上个Servlet的值，然后强转
        UserService userService=new UserServiceImpl();
        List<User> all = userService.findAll();//登入成功后就展示所有值

        PrintWriter writer = resp.getWriter();


        writer.println("<html>");
        writer.println("<head>");
        writer.println("<meta charset=\"UTF-8\">");
        writer.println("<title>Title</title>");
        writer.println("</head>");
        writer.println("<body>");
        writer.println("<h3>员工信息表</h3>");
        writer.println("<table border='1px' cellspacing='0' >");
        writer.println("<tr>");
            writer.println("    <td>id</td>");
            writer.println("    <td>username</td>");
            writer.println("    <td colspan='3'>操作</td>");
        writer.println("</tr>");
            for (User user : all) {
                writer.println("<tr>");
                writer.println("    <td>"+user.getId()+"</td>");
                writer.println("    <td>"+user.getUsername()+"</td>");
                writer.println("    <td><a href='/delete?id="+user.getId()+"'>删除</a></td>");  //打开deleteUser这个Servlet，并传递一个id值
                writer.println("    <td><a href='/insertUser.html'>增加</a></td>");     //打开insertUser页面，然后提交发出请求到Servlet
                writer.println("    <td><a href='/findById?id="+user.getId()+"'>修改<a><td>"); //路径是其他Servlet,通过id修改值，
                writer.println("</tr>");                                                       //不可以直接路径到网页页面，因为id无法传到页面，只能传到其他Servlet

            }
        writer.println("</table border='1'>");

        writer.println("</body>");
        writer.println("</html>");

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        }

    }

